/*
 * ds18b20.c
 *
 * Copyright 2022 dh33ex <dh33ex@riseup.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
 *
 *
 */

#ifndef __msp430_h_
    #include <msp430.h>
#endif
#include "ds18b20.h"


void reset_18B20(unsigned char pin) {
    P1DIR |= pin;
    P1OUT &= ~pin;
    __delay_cycles(500);
    P1OUT |= pin;
    P1DIR &= ~pin;
    __delay_cycles(500);
}


void send_18B20(unsigned char pin, char data) {
    char i;
    for (i = 8; i > 0; i--) {
        P1DIR |= pin;
        P1OUT &= ~pin;
        __delay_cycles(2);
        if (data & 0x01){
            P1OUT |= pin;
        }
        __delay_cycles(60);
        P1OUT |= pin;
        P1DIR &= ~pin;
        data >>= 1;
    }
}


unsigned int read_18B20(unsigned char pin) {
    unsigned int data=0;
    char i;
    /* read only first 2 bytes (temperature) */
    for (i = 16; i > 0; i--) {
        P1DIR |= pin;
        P1OUT &= ~pin;
        __delay_cycles(2);
        P1OUT |= pin;
        P1DIR &= ~pin;
        __delay_cycles(8);
        if (P1IN & pin) {
            data |= 0x8000;
        }
        data >>= 1;
        __delay_cycles(120);
    }
    return(data);
}


void set_resolution(unsigned char pin, unsigned char resolution) {
    unsigned char package;
    switch (resolution) {
        case 9:
            package = 0b00011111;
            break;

        case 10:
            package = 0b00111111;
            break;

        case 11:
            package = 0b01011111;
            break;

        case 12:
            package = 0b01111111;
            break;

        default:
            package = 0b00011111;       /* wrong resolution argument, set default */
            break;
    }

    reset_18B20(pin);
    send_18B20(pin, 0xcc);              /* send skip ROM command */
    send_18B20(pin, 0x4e);              /* send write scratchpad command */
    send_18B20(pin, 0x00);              /* send TH byte (unused) */
    send_18B20(pin, 0x00);              /* send TL byte (unused) */
    send_18B20(pin, package);           /* send configure byte */
    reset_18B20(pin);
    __delay_cycles(120);
}


float get_temp(unsigned char pin) {
    unsigned int temp;
    reset_18B20(pin);
    send_18B20(pin, 0xcc);              /* send skip ROM command */
    send_18B20(pin, 0x44);              /* send convert temperature command */
    __delay_cycles(1000000);

    reset_18B20(pin);
    send_18B20(pin, 0xcc);              /* send skip ROM command */
    send_18B20(pin, 0xbe);              /* send read scratchpad command */

    temp = read_18B20(pin);             /* read raw data */
    return((float) temp / 8.0);         /* calculate temperature */
}
